

var isIE = navigator.userAgent.indexOf('MSIE') != -1 && !window.opera;
function C(i,n,r){var e = document.createElement(i);if(n) e.id = n;if(r) e.className = r;return e;}
function setStyle(obj,attr){var str = '';for(str in attr){obj.style[str] = attr[str];}}
function addEvent(elem, type, handler,prop){if(isIE){elem.attachEvent('on' + type, handler);}else{elem.addEventListener(type, handler,!prop);}}
function each(arr,fn){for(var i=arr.length-1;i>=0;i--)fn(arr[i],i);}
function getClass(obj,sClass){
	if(obj.getElementsByClassName)return obj.getElementsByClassName(sClass);
	var result = [];
	var elem = obj.getElementsByTagName("*");
	each(elem,function(item,i){
		if(item.className==sClass)result.push(item);
	})
	return result;
}
var level;
var iNow;
(function(){
	var barrierArr = [],
		itarget,
		box = [],
		gameOver = false,
		boxSize = 40,
		minLeft = 0,
		maxLeft,
		minTop = 0,
		maxTop;
	window.setVar = function(go,lev){
		barrierArr.length = 0;
		box.length = 0;
		gameOver = go;
		itarget = {x:mapArr[lev].bt.x.slice(mapArr[lev].bt.x.length/2),y:mapArr[lev].bt.y.slice(mapArr[lev].bt.y.length/2)};
		maxLeft = mapArr[lev].land.cell*boxSize - boxSize;
		maxTop = mapArr[lev].land.len/mapArr[lev].land.cell*boxSize - boxSize;
	}
	window.Box = function(obj){
		this.obj = obj;
		this.x = obj.offsetLeft;
		this.y = obj.offsetTop;
		barrierArr.push(this);
		box.push(obj);
	}
	window.Box.prototype = {
		setX: function(newX){
			this.x = newX;
			this.obj.style.left = newX + 'px';
		},
		setY: function(newY){
			this.y = newY;
			this.obj.style.top = newY + 'px'
		},
		isPushed: function(direct){
			if(gameOver) return false;
			var shift;
			var newX = this.x + (Math.abs((shift = direct-38)) == 1? shift: 0)*boxSize;
			var newY = this.y + (Math.abs((shift = direct-39)) == 1? shift: 0)*boxSize;
			if(newX < minLeft || newX > maxLeft || newY < minTop || newY > maxTop){
				return false;
			}
			for(var i=0; i<barrierArr.length; i++){
				if(this == barrierArr[i]){
					continue;
				}
				if(newX == barrierArr[i].x && newY == barrierArr[i].y){
					return false;
				}
			}
			this.setX(newX);
			this.setY(newY);
			return true;
		}
	}
	window.Barrier = function(obj){
		this.obj = obj;
		this.x = obj.offsetLeft;
		this.y = obj.offsetTop;
		barrierArr.push(this);
	}
	window.Barrier.prototype = {
		isPushed: function(){
			return false;
		}
	}
	window.Boss = function(obj){
		this.obj = obj;
		this.x = obj.offsetLeft;
		this.y = obj.offsetTop;
	}
	window.Boss.prototype = {
		setX: function(newX){
			if(this.x == newX){
				return;
			}
			this.x = newX;
			this.obj.style.left = newX + 'px';
		},
		setY: function(newY){
			if(this.y == newY){
				return;
			}
			this.y = newY;
			this.obj.style.top = newY + 'px';
		},
		push: function(direct){
			var shift;
			var count = 0;
			var newX = this.x + (Math.abs((shift = direct-38)) == 1? shift: 0) * boxSize;
			shift == 1 && this.obj.className != 'r'
				&& (this.obj.className = 'r');
			shift == -1 && this.obj.className != 'l'
				&& (this.obj.className = 'l');
			var newY = this.y + (Math.abs((shift = direct-39)) == 1? shift: 0) * boxSize;
			if(newX < minLeft || newX > maxLeft || newY < minTop || newY > maxTop){
				return false;
			}
			for(var i=0; i<barrierArr.length; i++){
				if(newX == barrierArr[i].x && newY == barrierArr[i].y && barrierArr[i] && !barrierArr[i].isPushed(direct)){
					return;
				}
			}
			this.setX(newX);
			this.setY(newY);
			for(var i=0; i<box.length; i++){
				for(var j=0; j< itarget.x.length; j++){
					if(box[i].offsetLeft ==  itarget.x[j] && box[i].offsetTop == itarget.y[j]){
						count++;
					}
				}
			}
			if(count==box.length && !gameOver){
				iNow++;
				setStorage("iNow",iNow);
				gameOver = true;
				if(window.confirm("恭喜过关，确定进入下一关！")){
					next();
				}
			}
		}
	}
})();
function CreatObj(oCanv){
	var boss = new Boss(document.getElementById('boss'));
	var boxArr = getClass(oCanv, 'box');
	var target = getClass(oCanv, 'target');
	var barrierArr = getClass(oCanv, 'barrier');
	each(boxArr,function(item){
		new Box(item);
	})
	each(barrierArr,function(item){
		new Barrier(item);
	})
	document.onkeydown = function(event){
		event = event || window.event;
		boss.push(event.keyCode);
	}
}
function CreatGame(level){
	var oCanv = document.getElementById("wrap");
	oCanv.innerHTML = "";
	setVar(false,level);
	var aDiv = null;
	var oUl = C("ul","game");
	var aLi = null;
	var divLen = mapArr[level].bt.x.length + 1;
	for(var i=0; i<divLen; i++){
		aDiv = C("div");
		if(mapArr[level].bt.x[i]!=null){
			setStyle(aDiv,{left:mapArr[level].bt.x[i]+"px",top:mapArr[level].bt.y[i]+"px"});
		}
		i < mapArr[level].bt.x.length/2 && (aDiv.className = "box");
		i >= mapArr[level].bt.x.length/2 && i < mapArr[level].bt.x.length
		&& (aDiv.className = "target");
		if(i == divLen-1){
			aDiv.setAttribute("id","boss");
			setStyle(aDiv,{left:mapArr[level].boss.x+"px",top:mapArr[level].boss.y+"px"});
		}
		oCanv.appendChild(aDiv);
	};
	for(var i=0; i<mapArr[level].land.len; i++){
		aLi =C("li");
		for(var j=0; j<mapArr[level].land.barrier.length; j++){
			if(i+1 == mapArr[level].land.barrier[j]){
				aLi.className = "barrier";
			}
		}
		oUl.appendChild(aLi);
	};
	setStyle(oCanv,{width:mapArr[level].land.cell*40+"px",height:(mapArr[level].land.len/mapArr[level].land.cell)*40+"px"});
	setStyle(oUl,{width:mapArr[level].land.cell*40+"px",height:(mapArr[level].land.len/mapArr[level].land.cell)*40+"px"});
	oCanv.appendChild(oUl);
	CreatObj(oCanv);
}
function next(){
	var guan = document.getElementById("control").getElementsByTagName("span")[0];
	if(level == mapArr.length-1 || iNow==level){
		alert("请先过这一关！");
		return false;
	}else{
		level++;
		setStorage("level",level);
	}
	guan.innerHTML = "第"+(level+1)+"关";
	CreatGame(level);
}
//本地存储记录已过关卡
function setStorage(name,value,expires){
	if (window.localStorage) {
	    localStorage.setItem(name, value);
	} else {
	  var cookieStr = name + '=' + value;
		if(expires instanceof Date){
			cookieStr += ';expires=' + expires;
		}else{
			var oDate=new Date();
			oDate.setDate(oDate.getDate()+30);
			cookieStr += ';expires=' + oDate.toGMTString();
		}
		document.cookie = cookieStr;
	}
}
function getStorage(name){
	if(window.localStorage){
	return	localStorage.getItem(name);
	}else{
		var startIndex = document.cookie.indexOf(name);
		if(startIndex < 0)return '';
		var endIndex = document.cookie.indexOf(';', startIndex);
		if(endIndex < 0)endIndex = document.cookie.length;
		var cookieStr = document.cookie.substring(startIndex, endIndex);
		var result = cookieStr.split('=');
		return result[1];
	}
}
function rmStorage(name){
	var oDate = new Date();
	return window.localStorage?localStorage.removeItem(name):setStorage(name, '', oDate);
}